package com.mazaiting.generator.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mazaiting.common.core.domain.result.Option;
import com.mazaiting.common.core.domain.result.ResultCode;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.generator.converter.IProjectConverter;
import com.mazaiting.generator.domain.entity.ProjectEntity;
import com.mazaiting.generator.domain.form.ProjectForm;
import com.mazaiting.generator.domain.info.ProjectInfo;
import com.mazaiting.generator.domain.query.ProjectQuery;
import com.mazaiting.generator.domain.vo.ProjectVO;
import com.mazaiting.generator.mapper.IProjectMapper;
import com.mazaiting.generator.service.IProjectService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 项目服务实现
 *
 * @author 阿沐 babamu@126.com
 */
@Service
@RequiredArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<IProjectMapper, ProjectEntity> implements IProjectService {

    private final IProjectConverter projectConverter;

    @Override
    public BasePageVO<ProjectInfo> page(ProjectQuery projectQuery) {
        IPage<ProjectInfo> pageList = baseMapper.page(new Page<>(projectQuery.getPageNum(), projectQuery.getPageSize()), projectQuery);
        return BasePageVO.success(pageList);
    }

    @Override
    public ProjectVO getInfoById(Long id) throws EmptyObjectException {
        // 根据 ID 获取工程信息
        ProjectEntity projectEntity = getById(id);
        if (Objects.isNull(projectEntity)) {
            throw new EmptyObjectException(ResultCode.COMMON_FAIL.getCode(), "工程信息不存在");
        }
        // entity -> vo
        return projectConverter.entity2VO(projectEntity);
    }

    @Override
    public boolean save(ProjectForm projectForm) {
        // form -> entity
        ProjectEntity projectEntity = projectConverter.form2Entity(projectForm);
        projectEntity.setId(null);
        // 如果是选择的状态, 则要更新其他数据状态为 false
        if (projectEntity.getSelected()) {
            lambdaUpdate()
                    .set(ProjectEntity::getSelected, false)
                    .eq(ProjectEntity::getSelected, true)
                    .update();
        }
        return save(projectEntity);
    }

    @Override
    public boolean update(ProjectForm projectForm) throws EmptyObjectException {
        // 根据 ID 查询
        ProjectEntity projectEntity = getById(projectForm.getId());
        if (Objects.isNull(projectEntity)) {
            throw new EmptyObjectException(ResultCode.COMMON_FAIL.getCode(), "工程信息不存在");
        }
        // form -> entity
        projectConverter.form2Entity(projectForm, projectEntity);
        // 如果是选择的状态, 则要更新其他数据状态为 false
        if (projectEntity.getSelected()) {
            lambdaUpdate()
                    .set(ProjectEntity::getSelected, false)
                    .eq(ProjectEntity::getSelected, true)
                    .update();
        }
        return updateById(projectEntity);
    }

    @Override
    public List<Option<Long>> listOptions() throws BusinessException {
        // 获取选项列表
        List<Option<Long>> optionList = lambdaQuery()
                .select(ProjectEntity::getId, ProjectEntity::getName)
                .list()
                .stream().map(entity -> new Option<>(entity.getId(), entity.getName())).toList();
        if (CollectionUtils.isEmpty(optionList)) {
            throw new BusinessException(ResultCode.COMMON_FAIL.getCode(), "无可用工程, 请先添加工程");
        }
        return optionList;
    }
}